package com.handinfo.android.game;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.object.Actor;
import com.handinfo.android.core.object.Animation;
import com.handinfo.android.core.object.Scene;
import com.handinfo.android.core.resource.DWSerializableFactory;
import com.handinfo.android.core.resource.ResHead;
import com.handinfo.android.core.resource.ResourceManager;
import com.handinfo.android.utils.Tools;

public class GameEffect extends Actor
{
	public static GameEffect s_effect;
	public static final String URL_EFFECT_ANIM = "/anim/effect.anim";

	public static void initStaticResource()
	{
		ResHead head = new ResHead(DWSerializableFactory.RESOURCE_TYPE_ANIM,
				URL_EFFECT_ANIM, 1);
		s_effect = new GameEffect(
				(Animation) (ResourceManager.getInstance().loadResource(
						DWGameManager.getInstance().m_resourceStatic, head)));
	}

	public static void disposeStaticResource()
	{
		if (s_effect == null)
			return;
		ResHead head = new ResHead(DWSerializableFactory.RESOURCE_TYPE_ANIM,
				URL_EFFECT_ANIM, 1);
		ResourceManager.getInstance().removeResource(
				DWGameManager.getInstance().m_resourceStatic, head);
		s_effect.dispose();
		s_effect = null;
	}

	public int m_curKeyFrame = 0;
	public boolean m_doKeyFrame = false;;
	public int m_duration = 0;

	public GameEffect()
	{
		super();
		m_activeBox[0] = -20;
		m_activeBox[1] = -40;
		m_activeBox[2] = 20;
		m_activeBox[3] = 0;
	}

	public GameEffect(Animation anim)
	{
		super(anim);
		m_activeBox[0] = -20;
		m_activeBox[1] = -40;
		m_activeBox[2] = 20;
		m_activeBox[3] = 0;
	}

	public void init(Scene scene, int srcx, int srcy, int destx, int desty, int actionID, boolean doKeyFrame,
			int duration, boolean flipX)
	{
		m_scene = scene;
		m_layer = m_scene.m_actorMovableLayer;
		m_duration = duration;
		m_isOnce = (duration == DURATION_NONE);
		m_needDraw = true;
		m_actionCycle = true;
		m_enabled = true;
		m_visible = true;
		m_flipX = flipX;
		m_doKeyFrame = doKeyFrame;
		// test
		m_doKeyFrame = false;

		m_curKeyFrame = 0;
		m_posX = srcx;
		m_posY = srcy + 1;
		setAction(actionID, true);
		int frameLength = getFrameAmount(actionID);
		if (frameLength > 0) {
			m_vX = (destx - srcx) / (float) frameLength;
			m_vY = (desty + 1 - srcy) / (float) frameLength;
		}
	}

	/**
	 * 克隆给定的元件对象
	 * 
	 * @param actor
	 * @return
	 */
	public Actor clone(Actor actor)
	{
		GameEffect ca = new GameEffect();
		ca.m_scene = actor.m_scene;
		ca.m_anims = new Animation[actor.m_anims.length];
		System.arraycopy(actor.m_anims, 0, ca.m_anims, 0, actor.m_anims.length);
		ca.m_anim = actor.m_anim;
		ca.m_actionIDNow = actor.m_actionIDNow;
		ca.m_actionIDBefore = actor.m_actionIDBefore;
		ca.m_frameId = actor.m_frameId;
		ca.m_frameIndex = actor.m_frameIndex;
		ca.m_frameDuration = actor.m_frameDuration;
		ca.m_actionCycle = actor.m_actionCycle;
		ca.m_actionOver = actor.m_actionOver;
		ca.m_layer = actor.m_layer;
		ca.m_phylayer = actor.m_phylayer;
		ca.m_flag = actor.m_flag;
		ca.m_flipX = actor.m_flipX;
		ca.m_flipY = actor.m_flipY;
		ca.m_visible = actor.m_visible;
		ca.m_enabled = actor.m_enabled;
		// ca.m_movable = actor.m_movable;
		ca.m_needDraw = actor.m_needDraw;
		ca.m_posX = actor.m_posX;
		ca.m_posY = actor.m_posY;
		ca.m_vX = actor.m_vX;
		ca.m_vY = actor.m_vY;
		ca.m_collideBox = new short[actor.m_collideBox.length];
		System.arraycopy(actor.m_collideBox, 0, ca.m_collideBox, 0, actor.m_collideBox.length);
		ca.m_activeBox = new short[actor.m_activeBox.length];
		System.arraycopy(actor.m_activeBox, 0, ca.m_activeBox, 0, actor.m_activeBox.length);
		ca.m_parameters = new short[actor.m_parameters.length];
		System.arraycopy(actor.m_parameters, 0, ca.m_parameters, 0, actor.m_parameters.length);
		return ca;
	}

	/**
	 * 
	 */
	public void updatePos()
	{
		m_prevPosX = m_posX;
		m_prevPosY = m_posY;
		m_posX += m_vX;
		m_posY += m_vY;
		if (m_scene == null || m_activeBox == null) {
			return;
		}
		m_needDraw = (m_posX + m_activeBox[2] >= m_scene.m_cameraX)
				&& (m_posX + m_activeBox[0] <= m_scene.m_cameraX
						+ m_scene.m_cameraW)
				&& (m_posY + m_activeBox[3] >= m_scene.m_cameraY)
				&& (m_posY + m_activeBox[1] <= m_scene.m_cameraY
						+ m_scene.m_cameraH);
	}

	/**
	 * 指向动作下一帧
	 */
	public void nextFrame()
	{
		try {
			m_frameDuration++;
			if (m_anim == null
					|| m_frameId >= m_anim.m_actions[m_actionIDNow].length
					|| m_frameDuration < ((m_anim.m_actions[m_actionIDNow][m_frameId] & 0xFFFF) >> 10)) {
//				Tools.debugPrintln("GameEffect m_frameId >= length! m_actionIDNow = " + m_actionIDNow);
				return;
			}

			m_frameId++;

			// 关键帧有特殊行为
			if (m_doKeyFrame) {
				if (m_curKeyFrame < KEY_FRAME[m_actionIDNow].length) {
					if (m_frameId >= KEY_FRAME[m_actionIDNow][m_curKeyFrame]) {
						m_curKeyFrame++;
						doKeyFrame();
					}
				}
			}

			if (m_frameId >= m_anim.m_actions[m_actionIDNow].length) {
				m_actionOver = true;
				if (m_actionCycle) {
					m_frameId = 0;
				}
				else {
					m_frameId--;
				}
				// 一次性元件
				if (m_isOnce) {
					m_enabled = false;
				}
			}

			m_frameIndex = (m_anim.m_actions[m_actionIDNow][m_frameId] & 0x03FF);
			m_frameDuration = 0;
		}
		catch (Exception ex) {
			m_enabled = false;
			Tools.debug(ex);
		}
	}
	
	

	@Override
	public void logic()
	{
		super.logic();
		if (m_duration == 0) {
			m_enabled = false;
		}
		else if (m_duration > 0) {
			m_duration--;
		}
	}

	public void doKeyFrame()
	{
	}

	/**
	 * 获得剩余帧数
	 * 
	 * @return
	 */
	public int getFrameLeft()
	{
		try {
			int frameLeft = m_anim.m_actions[m_actionIDNow].length - 1 - m_frameId;
			return frameLeft;
		}
		catch (Exception e) {
			Tools.debug(e);
			return 0;
		}
	}
	
	/**
	 * 检查光效是否需要X翻转
	 * @param actor
	 * @return
	 */
	public static boolean checkFlipX(GameActor actor)
	{
		if (actor == null)
			return false;
		return actor.m_flipX || actor.m_actionIDNow % GameActorConstants.DIRECT_NUM == GameActorConstants.DIRECT_W;
	}

	public static final byte[][] KEY_FRAME = new byte[][] {

	};

	/**
	 * 无持续时间，代表该光效在动画结束后失效
	 */
	public static final int DURATION_NONE = -1;
}
